//
//  ResetpwdViewController.swift
//  Auscultation
//
//  Created by 张文博 on 2021/4/19.
//

import UIKit

class ResetpwdViewController: BaseVC {
    var resetSuccessClouse: ((String,String)->Void)?
    
    var view1: UIImageView!
    var view2: UIImageView!
    
    var dl_label: UILabel!
    var zc_label: UILabel!
    
    var yhm: LGFLoginImageTitleView!
    var mm: LGFLoginImageTitleView!
    var yzm: LGFLoginImageTitleView!
    
    var loginBtn: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        zhuce()
        self.navTitle = ""
//        let style = LGFNavigationBarStyle.lgf()
//        style.lgf_LeftBtnImageLight = UIImage(named: "darkback")!
//        self.lgf_NavigationBar.lgf_line?.isHidden = true
//        self.lgf_NavigationBar.lgf_Reload()

        
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension ResetpwdViewController {
    
    func initUI() {
        view1 = UIImageView()
        view1.image = UIImage(named: "login_header_repeat")
        view.addSubview(view1)
        view1.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(view.lgf_Width * 0.7)
        }
        view2 = UIImageView.init(image: #imageLiteral(resourceName: "login_header2@1.5x"))
        view.addSubview(view2)
        view2.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(view.lgf_Width * 0.7)
        }
        
        dl_label = UILabel.init()
        dl_label.text = "登录"
        dl_label.font = UIFont.boldSystemFont(ofSize: 18)
        dl_label.isUserInteractionEnabled = true
        dl_label.lgf_AddTap(target: self, action: #selector(LoginVC.denglu))
        view.addSubview(dl_label)
        dl_label.snp.makeConstraints { (make) in
            make.top.equalTo(view1.snp.bottom).offset(20)
            make.height.equalTo(0)
            make.centerX.equalToSuperview().multipliedBy(0.565)
        }

        zc_label = UILabel.init()
        zc_label.text = "注册"
        zc_label.font = UIFont.boldSystemFont(ofSize: 18)
        zc_label.isUserInteractionEnabled = true
        zc_label.lgf_AddTap(target: self, action: #selector(LoginVC.zhuce))
        view.addSubview(zc_label)
        zc_label.snp.makeConstraints { (make) in
            make.top.equalTo(view1.snp.bottom).offset(20)
            make.height.equalTo(0)
            make.centerX.equalToSuperview().multipliedBy(1.435)
        }
        // 12345678901 xyadmin@v2
        yhm = LGFLoginImageTitleView.init(#imageLiteral(resourceName: "yonghuming@1.5x"), nil, left: 0, placeholder: "用户名/手机号/账号", text: "", false)
        yhm.delegate = self
        view.addSubview(yhm)
        yhm.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(32.lgf_W())
            make.right.equalToSuperview().offset(-32.lgf_W())
            make.top.equalTo(dl_label.snp.bottom).offset(15.lgf_H())
            make.height.equalTo(44.lgf_H())
        }
        
        mm = LGFLoginImageTitleView.init(#imageLiteral(resourceName: "mima@1.5x"), nil, left: 0, placeholder: "密码", text: "", false)
        mm.delegate = self
        mm.textField.isSecureTextEntry = true
        view.addSubview(mm)
        mm.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(32.lgf_W())
            make.right.equalToSuperview().offset(-32.lgf_W())
            make.top.equalTo(yhm.snp.bottom).offset(15.lgf_H())
            make.height.equalTo(44.lgf_H())
        }
        
        yzm = LGFLoginImageTitleView.init(#imageLiteral(resourceName: "yanzhengma@1.5x"), nil, left: 0, placeholder: "输入验证码", text: "", false)
        yzm.delegate = self
        view.addSubview(yzm)
        yzm.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(32.lgf_W())
            make.right.equalToSuperview().offset(-32.lgf_W())
            make.top.equalTo(mm.snp.bottom).offset(15.lgf_H())
            make.height.equalTo(44.lgf_H())
        }
        
        let hqyzm = UIButton.init(type: .custom)
        hqyzm.setTitle("获取验证码", for: .normal)
        hqyzm.setTitleColor(UIColor.init(lgf_HexString: "0F88F2"), for: .normal)
        hqyzm.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        hqyzm.layer.cornerRadius = 5
        hqyzm.layer.borderColor = UIColor.init(lgf_HexString: "0F88F2")?.cgColor
        hqyzm.layer.borderWidth = 1
        hqyzm.addTarget(self, action: #selector(LoginVC.yanzhengma(_:)), for: .touchUpInside)
        yzm.addSubview(hqyzm)
        hqyzm.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(-10)
            make.right.equalToSuperview().offset(-10)
            make.width.equalTo(90)
        }
        
        loginBtn = UIButton.init(type: .custom)
        loginBtn.backgroundColor = UIColor.init(lgf_HexString: EWEDarkMode.c_0F88F2)!
        loginBtn.setTitle("重置密码", for: .normal)
        loginBtn.setTitleColor(EWEDarkMode.white(), for: .normal)
        loginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        loginBtn.layer.cornerRadius = 8
        loginBtn.layer.masksToBounds = true
        loginBtn.addTarget(self, action: #selector(LoginVC.login(_:)), for: .touchUpInside)
        view.addSubview(loginBtn)
        loginBtn.snp.makeConstraints { (make) in
            make.top.equalTo(yzm.snp.bottom).offset(35.lgf_H())
            make.centerX.equalToSuperview()
            make.width.equalToSuperview().offset(-80.lgf_W())
            make.height.equalTo(44.lgf_H())
        }
    }
    
    @objc func zhuce() {
        view1.isHidden = false
        view2.isHidden = true
        yzm.isHidden = false
        yzm.snp.updateConstraints { (make) in
            make.height.equalTo(44.lgf_H())
        }
        loginBtn.setTitle("重置密码", for: .normal)
        loginBtn.lgf_ViewName = "重置密码"
        dl_label.textColor = UIColor.init(lgf_HexString: "CFD4D9")
        zc_label.textColor = UIColor.init(lgf_HexString: "222222")
    }

    
    @objc func login(_ sender: UIButton) {
        if yhm.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请输入用户名/手机号/账号")
            return
        }
        if mm.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请输入密码")
            return
        }
        
        if sender.lgf_ViewName == "重置密码" {
            if yzm.textField.text!.lgf_IsBlank() {
                SVProgressHUD.showError("请输入验证码")
            }
            let paramet: [String: String] = [
                "phone" : yhm.textField.text!,
                "password" : mm.textField.text!,
                "code" : yzm.textField.text!]
            EWERequest.lgf("signup", paramet, .put, JSONEncoding.default) { [weak self] (data) in
                guard let self = self else { return  }
                self.yzm.textField.text = ""
                self.resetSuccessClouse?(self.yhm.textField.text!,self.mm.textField.text!)
                self.navigationController?.popViewController(animated: true)
            } _: { (error) in
                SVProgressHUD.showError(error)
            }
        }
    }
    
    @objc func yanzhengma(_ sender: UIButton) {
        var num = 60
        sender.isEnabled = false
        let timer = Timer.lgf_RunTimer(self, S: 1.0) { (timer) in
            if num == 1 {
                timer?.lgf_StopTimer()
                sender.isEnabled = true
                sender.setTitle("获取验证码", for: .normal)
            } else {
                num -= 1
                sender.setTitle(num.description + "s", for: .normal)
            }
        }
        
        let paramet: [String: String] = [
            "phone" : yhm.textField.text!,
            "type" : "0"]
        EWERequest.lgf("sms", paramet, .post, JSONEncoding.default) { (data) in
        } _: { (error) in
            timer.lgf_StopTimer()
            sender.isEnabled = true
            sender.setTitle("获取验证码", for: .normal)
            SVProgressHUD.showError(error)
        }
    }


}

extension ResetpwdViewController: LGFLoginImageTitleViewDelegate {
    func selectLGFLoginImageTitleView(_ view: LGFLoginImageTitleView) {
        self.view.endEditing(true)
        
    }
}

